// 这份代码专门处理逻辑层的操作
const consultDao = require('../dao/consultDao');

const powerService = require('./powerService');

const consultService = {
    /*
     * 查询所有咨询记录接口
     */
    async getAllConsultList(uid, userType) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看咨询记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        let functions = {
            "咨询师": consultDao.getAllConsultListOfCounselor,
            "学院书记": consultDao.getAllConsultListOfCollegeMaster
        };
        // 如果 userType 存在于 functions 里 则执行对应的函数
        let func = functions[userType] ? functions[userType] : consultDao.getAllConsultList;
        try {
            let getResult = await func(uid);
            if (getResult.length == 0) {
                result = { success: false, msg: "查询为空" }
            } else {
                result = { success: true, msg: "查询成功", data: getResult }
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" }
        }
        return result;
    },
    /*
     * 新增咨询记录
     */
    async addConsultRecord(uid, userType, data) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '新建咨询记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let func = consultDao.addConsultRecord;
            if (userType == '咨询师') {
                data.counselorId = uid;
                func = consultDao.addConsultSelfRecord;
            }
            let addResult = await func(data);
            result = { success: true, msg: "插入成功", insertId: addResult.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，插入失败" };
        }
        return result;
    },
    async getConsultRecordInfo(uid, userType, consultationId) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看咨询记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        let functions = {
            "咨询师": consultDao.getConsultRecordInfoOfCounselor,
            "学院书记": consultDao.getConsultRecordInfoOfCollegeMaster
        };
        // 根据身份不同调用不同的权限
        let func = functions[userType] ? functions[userType] : consultDao.getConsultRecordInfo;
        try {
            let findResult = await func(consultationId, uid);
            if (findResult.length == 0) {
                result = { success: false, msg: "查询为空" }
            } else {
                result = { success: true, msg: "查询成功", insertId: findResult[0] }
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" }
        }
        return result;
    },
    /*
     * 根据学生id获取咨询记录
     */
    async getConsultRecordByStudent(uid, userType, studentId) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看学生信息');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 根据身份不同调用不同的权限
        let funcMap = {
            "咨询师": consultDao.getConsultRecordByCounselor,
            "学院书记": consultDao.getConsultRecordByStudentOfFaculty
        };
        let func = consultDao.getConsultRecordByStudent;
        for (i in funcMap) {
            if (userType == i) {
                func = funcMap[i];
            }
        }
        // let func = (userType == '咨询师' || userType == '学院书记') ? consultDao.getConsultRecordByStudentOfFaculty : consultDao.getConsultRecordByStudent;
        try {
            result = await func(studentId, uid);
            result = { success: true, msg: "查询成功", result };
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 修改咨询记录
     */
    async updateConsultRecordInfo(uid, userType, data) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看咨询记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let updateResult = await consultDao.updateConsultRecordInfo(data);
            result = { success: true, msg: "修改成功" }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，插入失败" }
        }
        return result;
    },
    /*
     * 删除咨询记录接口
     */
    async deleteConsultRecord(uid, userType, consultationId) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '新建咨询记录');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await consultDao.deleteConsultRecord(consultationId);
            result = { success: true, msg: "删除成功" }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，删除失败" }
        }
        return result;
    },
    /*
     * 获取所有咨询师列表
     */
    async getAllCounselorList() {
        let result;
        try {
            result = await consultDao.getAllCounselorList();
            result = { success: true, msg: "查询成功", allCounselorList: result };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },
    /*
     * 获取所有咨询方式列表
     */
    async getAllConsultationMethods() {
        let result;
        try {
            result = await consultDao.getAllConsultationMethods();
            result = { success: true, msg: "查询成功", allConsultationMethods: result };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },

    /*
     * 获取所有咨询问题类型列表
     */
    async getAllIssueTypes() {
        let result;
        try {
            result = await consultDao.getAllIssueTypes();
            result = { success: true, msg: "查询成功", allIssueTypes: result };
        } catch (error) {
            result = { success: false, msg: "查看数据库时出错，查看失败" };
        }
        return result;
    },
    
    /*
     * 获取所有咨询主题列表
     */
    // async getAllConsultationTopics() {
    //     let result;
    //     try {
    //         result = await consultDao.getAllConsultationTopics();
    //         result = { success: true, msg: "查询成功", allConsultationTopics: result };
    //     } catch (error) {
    //         result = { success: false, msg: "查看数据库时出错，查看失败" };
    //     }
    //     return result;
    // }
};

module.exports = consultService;